package com.banzhi.shop.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.banzhi.shop.R;
import com.banzhi.shop.adapter.CommonHeadAdapter;
import com.banzhi.shop.data.PartnerProfitBean;
import com.banzhi.shop.model.CallBack.PartnerShouYiCallBack;
import com.banzhi.shop.model.Impl.PartnerShouYiImpl;
import com.banzhi.shop.utils.EndlessRecyclerOnScrollListener;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.ShareUtils;
import com.banzhi.shop.utils.ToastUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/3.
 */

public class PartnerProfit_YifaFragment extends Fragment implements PartnerShouYiCallBack, SwipeRefreshLayout.OnRefreshListener {
    private LinearLayoutManager LayoutManager;
    private RelativeLayout rlnodata;
    private View view;
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private int page = 1;
    private PartnerShouYiImpl model;

    private List<PartnerProfitBean> mPartnerProfitBeans = new ArrayList<>();
    private CommonHeadAdapter<PartnerProfitBean> adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_shouyi__daifa, container, false);
        setinits(view);
        setlisteners();
        model = new PartnerShouYiImpl();
        LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
        model.getData(page + "", "1", this);
        return view;
    }

    private void setinits(View view) {
        refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.srl_listview);
        refreshLayout.setColorSchemeResources(R.color.themeColor);
        refreshLayout.setProgressBackgroundColorSchemeResource(R.color.colorWhite);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        LayoutManager = new LinearLayoutManager(getActivity());
        LayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(LayoutManager);
        rlnodata = (RelativeLayout) view.findViewById(R.id.rl_nodata);

    }

    private void setlisteners() {
        refreshLayout.setOnRefreshListener(this);
        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(LayoutManager) {
            @Override
            public void onLoadMore() {
                page++;
                LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
                model.getData(page + "", "1", PartnerProfit_YifaFragment.this);
            }
        });

    }


    @Override
    public void onRefresh() {
        LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
        page = 1;
        model.getData(page + "", "1", this);

    }

    @Override
    public void ongetDataSuccess(Object object) {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }
        List<PartnerProfitBean> PartnerProfitBean = (List<PartnerProfitBean>) object;
        if (page == 1) {
            mPartnerProfitBeans.clear();
        } else {
            if (PartnerProfitBean.size() == 0) {
//                ToastUtils.makeToastShort("没有更多了");
                return;
            }
        }
      //  mPartnerProfitBeans.clear();
        mPartnerProfitBeans.addAll(PartnerProfitBean);

        if (mPartnerProfitBeans.size() == 0) {
            rlnodata.setVisibility(View.VISIBLE);
        } else {
            rlnodata.setVisibility(View.GONE);
        }
        if (adapter == null) {
            adapter = new CommonHeadAdapter<PartnerProfitBean>(getActivity(), R.layout.item_daifa_partner, mPartnerProfitBeans) {
                @Override
                public void convert(final ViewHolder holder, final PartnerProfitBean bean, int position) {

                    holder.setText(R.id.tv_order, bean.getOrdersn());
                    holder.setText(R.id.tv_title, bean.getTitle());
                    holder.setText(R.id.tv_profit_manage, bean.getCpcome());
                    holder.getTextView(R.id.tv_fuzhi).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ShareUtils.copyLianjie(getActivity(), bean.getOrdersn());
                        }
                    });
                    holder.setText(R.id.tv_title, bean.getTitle());
                    LogUtils.d("管理利益",bean.getCpcome());
                    holder.setText(R.id.tv_time, bean.getOrdertime());
                    if ( "1".equals(bean.getOrderstatus())) {
                        holder.setText(R.id.tv_statu, "付款待发货");
                    } else if ( "0".equals(bean.getOrderstatus())) {
                        holder.setText(R.id.tv_statu, "下单未付款");
                    } else if ( "2".equals(bean.getOrderstatus())) {
                        holder.setText(R.id.tv_statu, "发货待收货");
                    } else if ( "3".equals(bean.getOrderstatus())) {
                        holder.setText(R.id.tv_statu, "已完成");
                    } else if ( "-1".equals(bean.getOrderstatus())) {
                        holder.setText(R.id.tv_statu, "关闭");
                    } else if ( "-2".equals(bean.getOrderstatus())) {
                        holder.setText(R.id.tv_statu, "关闭");

                    }
                }

                @Override
                public void headconvert(HeaderViewHolder holder) {

                }
            };
            recyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }



    }

    @Override
    public void ongetDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(getActivity(), jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));

    }

}
